// Y轴设置
import React, { useState } from 'react';
import { Switch, Input } from 'antd';
import Editor from 'src/pages/AnalyseManage/EditReport/components/EditorStyleType/Editor';
import FormItem from 'antd/lib/form/FormItem';

interface IProps {
	config: any;
	onValuesChange: ( chartType: string, key: string, values: Object, configType: string ) => void;
	form: any;
}

const YAxis = ( props: IProps ) => {
	const { config, onValuesChange, form } = props;

	const fontConfig = [ 'fontFamily', 'fontSize', 'fontColor', 'fontBold', 'fontItalic' ];
	const [ isShowYLineName, setIsShowYLineName ] = useState(
		config.style && config.style.hasOwnProperty( 'isShowYLineName' ) ? config.style.isShowYLineName : false
	);
	const [ isShowYLine, setIsShowYLine ] = useState(
		config.style && config.style.hasOwnProperty( 'isShowYLine' ) ? config.style.isShowYLine : false
	);
	const [ isHiddenYGridLine, setIsHiddenYGridLine ] = useState(
		config.style && config.style.hasOwnProperty( 'isHiddenYGridLine' ) ? config.style.isHiddenYGridLine : false
	);

	const onChange = ( checked: boolean, name: string, cb: any ) => {
		cb && cb();
		form.setFieldsValue( { [name]: checked } );
		onValuesChange( config.type, config.key, { [name]: checked }, 'style' );
	};

	const onBlur = ( value: any, name: string ) => {
		onValuesChange( config.type, config.key, { [name]: form.getFieldsValue()[name] }, 'style' );
	};

	return (
		<div>
			<FormItem label="隐藏Y轴网格线" name="isHiddenYGridLine" labelAlign="left" labelCol={{ span: 10 }} wrapperCol={{ span: 14 }}>
				<Switch
					checked={isHiddenYGridLine}
					onChange={( checked ) => onChange( checked, 'isHiddenYGridLine', setIsHiddenYGridLine( checked ) )}
				></Switch>
			</FormItem>
			<FormItem label="是否显示轴线" name="isShowYLine" labelAlign="left" labelCol={{ span: 10 }} wrapperCol={{ span: 14 }}>
				<Switch
					checked={isShowYLine}
					onChange={( checked ) => onChange( checked, 'isShowYLine', setIsShowYLine( checked ) )}
				></Switch>
			</FormItem>
			{isShowYLine && (
				<>
					<FormItem label="是否显示轴名称" name="isShowYLineName" labelAlign="left" labelCol={{ span: 10 }} wrapperCol={{ span: 14 }}>
						<Switch
							checked={isShowYLineName}
							onChange={( checked ) => onChange( checked, 'isShowYLineName', setIsShowYLineName( checked ) )}
						></Switch>
					</FormItem>

					{isShowYLineName && (
						<>
							<FormItem label="Y轴名称" name="chartYAxisName">
								<Input
									onBlur={( value ) => onBlur( value, 'chartYAxisName' )}
									onPressEnter={( value ) => onBlur( value, 'chartYAxisName' )}
								></Input>
							</FormItem>
							<FormItem label="字体" name="chartYLineFont">
								<Editor
									config={config}
									form={form}
									onValuesChange={onValuesChange}
									fontFamilyName="YLineFontFamily"
									fontSizeName="YLineFontSize"
									fontColorName="YLineFontColor"
									boldName="YLineFontBold"
									italicName="YLineFontItalic"
									fontConfig={fontConfig}
								/>
							</FormItem>
						</>
					)}
				</>
			)}
		</div>
	);
};

export default React.memo( YAxis );
